package com.ship.dispatch.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ship.common.core.web.domain.JsonResult;
import com.ship.common.log.annotation.Log;
import com.ship.common.log.enums.BusinessType;
import com.ship.common.security.utils.SecurityUtils;
import com.ship.dispatch.bean.SpArrangeSchemeDetail;
import com.ship.dispatch.model.Vo.SpArrangeStatisticsVo;
import com.ship.dispatch.model.dto.ArrangeSchemeChartSimpleDto;
import com.ship.dispatch.model.dto.SchemeChartSimpleDto;
import com.ship.dispatch.model.dto.SpArrangeSchemeDetailStatusDto;
import com.ship.dispatch.service.ExportService;
import com.ship.dispatch.service.SpArrangeSchemeDetailService;
import com.ship.dispatch.service.SpArrangeSchemeService;
import com.ship.dispatch.util.NumberFormatUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Field;
import java.text.DecimalFormat;
import java.util.LinkedList;
import java.util.List;

import static com.ship.dispatch.util.NumberFormatUtil.decimalFormat;

/**
 * 船货排布管理Controller
 *
 * @author zhangyh
 * @create 2023/11/03
 */
@RestController
@RequestMapping("/spArrange" )
@Api(value = "disSpArrange", tags = "船货排布前端控制器")
public class SpArrangeSchemeController {

    @Autowired
    private SpArrangeSchemeService spArrangeSchemeService;

    @Autowired
    private SpArrangeSchemeDetailService spArrangeSchemeDetailService;

    @Autowired
    private ExportService exportService;

    /**
     * 获取甘特图信息
     *
     * @return
     */
    @ApiOperation(value = "获取甘特图信息")
    @PostMapping("/scheme/chart")
    public JsonResult<SpArrangeSchemeDetail> getArrangeSchemeChart(@RequestBody SchemeChartSimpleDto dto) {
        return spArrangeSchemeService.getArrangeSchemeChart(dto);
    }

    /**
     * 获取可用船只甘特图信息
     *
     * @return
     */
    @ApiOperation(value = "获取可用船只甘特图信息")
    @PostMapping("/available/scheme/chart")
    public JsonResult<SpArrangeSchemeDetail> getAvailableArrangeSchemeChart(@RequestBody @Validated ArrangeSchemeChartSimpleDto dto) {
        return spArrangeSchemeService.getAvailableArrangeSchemeChart(dto);
    }

    /**
     * 获取方案详情分页
     *
     * @return
     */
    @ApiOperation(value = "获取方案详情分页")
    @PostMapping("/scheme/detail/page")
    public JsonResult<SpArrangeSchemeDetail> getSchemeDetailPage(@RequestBody @Validated SpArrangeSchemeDetail dto) {
        return spArrangeSchemeService.getSchemeDetailPage(dto);
    }

    /**
     * 方案详情分页数据导出
     *
     * @return
     */
    @ApiOperation(value = "方案详情分页数据导出")
    @PostMapping("/scheme/detail/page/export")
    @Log(title = "方案详情分页数据导出",businessType = BusinessType.EXPORT)
    public JsonResult getSchemeDetailPageExport(@RequestBody SpArrangeSchemeDetail dto) {
        if(StringUtils.isBlank(dto.getContractName())){
            dto.setContractName(null);
        }
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("scheme_number");
        if(dto.getIds() != null && dto.getIds().size() > 0){
            queryWrapper.in("id",dto.getIds());
        }
        List<SpArrangeSchemeDetail> exportList = spArrangeSchemeDetailService.pageExportList(dto, queryWrapper, new LinkedList<>());
        for (SpArrangeSchemeDetail detail : exportList) {
            if (detail.getStorageCapacity()!=null) {
                String result = null;
                DecimalFormat decimalFormat = new DecimalFormat("#,###.###");
                result = decimalFormat.format(detail.getStorageCapacity());
                result = result+" m³";
                detail.setStorageCapacityString(result);
            }
            if (null != detail.getTradeType()){
                if (1 == detail.getTradeType()){
                    detail.setTradeTypeStr("长协");
                }
                if (2 == detail.getTradeType()){
                    detail.setTradeTypeStr("中期");
                }
                if (3 == detail.getTradeType()){
                    detail.setTradeTypeStr("现货");
                }
            }
            if (null != detail.getTradeType()){
                if (1 == detail.getTradeBody()){
                    detail.setTradeBodyStr("国贸");
                }
                if (2 == detail.getTradeBody()){
                    detail.setTradeBodyStr("新贸");
                }
            }

            if (detail.getRealityUnloadingCapacity() !=null){
                String result = null;
                DecimalFormat decimalFormat = new DecimalFormat("#,###.###");
                result = decimalFormat.format(detail.getRealityUnloadingCapacity());
                result = result+" MMBtu";
                detail.setRealityUnloadingCapacityStr(result);
            }
            if (detail.getLoadingCapacity()!=null){
                String result = null;
                result = decimalFormat.format(detail.getLoadingCapacity());
                if(detail.getLoadingCapacityUnit() != null){
                    result = (result + (detail.getLoadingCapacityUnit().intValue() == 1 ? " MMBtu"
                            : (detail.getLoadingCapacityUnit().intValue() == 2 ? " m³" : " MT")));
                }
                detail.setLoadingCapacityString(result);
            }
            if (null != detail.getExecuteStatus()){
                if (1 == detail.getExecuteStatus()){
                    detail.setExecuteStatusStr("未下发");
                }
                if (2 == detail.getExecuteStatus()){
                    detail.setExecuteStatusStr("已下发");
                }
                if (3 == detail.getExecuteStatus()){
                    detail.setExecuteStatusStr("已完成");
                }
                if (4 == detail.getExecuteStatus()){
                    detail.setExecuteStatusStr("已终止");
                }
            }



//            if(detail.getLoadingCapacity() != null){
//                detail.setLoadingCapacityString(NumberFormatUtil.numericFormatting(detail.getLoadingCapacity())+" m³");
//            }
//            if(detail.getSingleGoodsQuantity() != null){
//                detail.setSingleGoodsQuantityString(NumberFormatUtil.numericFormatting(detail.getSingleGoodsQuantity())+" MMBtu");
//            }
//            if(detail.getOilConsumption() != null){
//                detail.setOilConsumptionString(NumberFormatUtil.numericFormatting(detail.getOilConsumption()));
//            }
        }
        Field[] fields = SpArrangeSchemeDetail.class.getDeclaredFields();
        String url = exportService.pageExport(exportList,fields,exportService.listStrByModuleAndUser
                ("sp_arrange_scheme_detail", SecurityUtils.getUserId()), "船货排布详情信息");
        return JsonResult.success(url);
    }

    /**
     * 船货排布统计信息
     * @return
     */
    @GetMapping("/top/statistics")
    @ApiOperation(value = "船货排布统计信息")
    public JsonResult<SpArrangeStatisticsVo> topStatistics(){
        return spArrangeSchemeService.topStatistics();
    }

    /**
     * 船货排布状态变更
     * @return
     */
    @PostMapping("/status/update")
    @ApiOperation(value = "船货排布状态变更")
    @Log(title = "船货排布状态变更",businessType = BusinessType.UPDATE)
    public JsonResult statusUpdate(@RequestBody @Validated SpArrangeSchemeDetailStatusDto dto){
        return spArrangeSchemeService.statusUpdate(dto);
    }

    /**
     * 船货排布方案详情
     * @return
     */
    @GetMapping("/detail/{detailId}")
    @ApiOperation(value = "船货排布方案详情")
    public JsonResult<SpArrangeSchemeDetail> detail(@PathVariable(value = "detailId") Long detailId){
        return spArrangeSchemeService.getDetail(detailId);
    }
}
